package com.lalosoft.twitter;

import winterwell.jtwitter.Twitter;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lalosoft.twitter.config.OAuthHelper;

/**
 * Ejemplo de Twitter para Android.
 * Links utiles:
 * OAuth: http://marakana.com/forums/android/examples/312.html
 * Steps: http://www.vogella.de/blog/2010/02/15/twitter-android/
 * Dev: https://dev.twitter.com/apps
 * @author lalosoft
 *
 */
public class TwitterDemoActivity extends Activity implements OnClickListener {
    
    static final String TAG = "MyTwitter";
    
    Twitter twitter;
    
    Button buttonUpdate;
    Button buttonPrefs;
    EditText textStatus;
    private OAuthHelper helper;
    
    //Dialog
	private Button customDialog_Update;
	private Button customDialog_Dismiss;
	private EditText username;
	private EditText password;
	private static final int CUSTOM_DIALOG_ID = 0;
	
	private boolean connected = false;
	
	private Context CTX = TwitterDemoActivity.this;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        helper = OAuthHelper.getInstance(this);
     // find views by id
        buttonUpdate = (Button) findViewById(R.id.buttonUpdate);
        textStatus = (EditText) findViewById(R.id.textStatus);
        
//     // find Dialog views
//        customDialog_Update = (Button) findViewById(R.id.dialogupdate);
//        customDialog_Dismiss = (Button) findViewById(R.id.dialogdismiss);
//        username = (EditText) findViewById(R.id.dialogedittext);
//        password = (EditText) findViewById(R.id.dialogpassword);
        
     // Add listener
        buttonUpdate.setOnClickListener(this);
        if(!isConfigured()){
        	Toast.makeText(CTX, "No username stored", Toast.LENGTH_SHORT).show();
        } else {
        	connect();
        	
        }
    }
    
    // Initialize twitter
    private void connect(){
//    	twitter = new Twitter(helper.getUserName(), helper.getOAuthClient());
    	twitter = new Twitter("euse_mar", helper.getOAuthClient());
    	connected = true;
    }
    
    private boolean isConfigured(){
    	return !helper.getUserName().equals("");
    }
    
    @Override
    public void onClick(View src) {
      
      String status = textStatus.getText().toString();
      
      Log.d(TAG, "Clicked on "+ status);
      
      if(!connected) connect();
      
      
      // Toast
      Toast.makeText(this, textStatus.getText(), Toast.LENGTH_LONG).show();
     
      // set twitter status 
//      twitter.setStatus(status);
      twitter.updateStatus(status);
      
      //reset status string
      textStatus.setText("");
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
    }
    
    // Called when menu item is selected //
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
      
      switch(item.getItemId()){
      
      case R.id.menuPrefs:
        // Launch Prefs activity
//        Intent i = new Intent(CTX, Prefs.class);
//        startActivity(i);
//        Log.d(TAG, "MenuPrefs starting Prefs");
//        Toast.makeText(CTX, textStatus.getText(), Toast.LENGTH_LONG).show();
    	  showDialog(CUSTOM_DIALOG_ID);
        break;
        
      }    
      return true;
    }
    
    @Override
	protected Dialog onCreateDialog(int id) {
	 // TODO Auto-generated method stub
	 Dialog dialog = null;;
	    switch(id) {
		    case CUSTOM_DIALOG_ID:{
		    	 dialog = new Dialog(CTX);
		 
			     dialog.setContentView(R.layout.custom_dialog);
			     dialog.setTitle("Enter your mail and password");
			     username = (EditText)dialog.findViewById(R.id.dialogedittext);
			     password = (EditText) dialog.findViewById(R.id.dialogpassword);
			     customDialog_Update = (Button)dialog.findViewById(R.id.dialogupdate);
			     customDialog_Dismiss = (Button)dialog.findViewById(R.id.dialogdismiss);
			     
			     customDialog_Update.setOnClickListener(customDialog_UpdateOnClickListener);
			     customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener);
		     
		     }break;
	    }
	    return dialog;
	}  
    
    
	   private Button.OnClickListener customDialog_UpdateOnClickListener = new Button.OnClickListener(){
			 
			 @Override
			 public void onClick(View arg0) {
				 Toast.makeText(CTX, "Saved!", Toast.LENGTH_LONG).show();
				 dismissDialog(CUSTOM_DIALOG_ID);
				 OAuthHelper.getInstance(CTX).setUserName(username.getText().toString());
				 OAuthHelper.getInstance(CTX).save();
			 }
			    
	   };

		private Button.OnClickListener customDialog_DismissOnClickListener = new Button.OnClickListener(){
		
				 @Override
				 public void onClick(View arg0) {
					 dismissDialog(CUSTOM_DIALOG_ID);
				 }
				    
		};
}